/*********************************************************************************
 * @name: common functions
 * @description:  Call functions of website
 * @author: (c) XGOON MEDIA VIET NAM (http://xgoon.com - contact@xgoon.com)
 * @version: 1.0
 *********************************************************************************/



/****************************
 ***** Global Namespace *****
 ****************************/
var XGOON = XGOON = XGOON || {
    isLog: true
};

/*************************************
 ***** Main functions start here *****
 *************************************/
jQuery(document).ready(function() {
    with (XGOON)
    {
        exerciseDo();
    }
});

//
jQuery(window).load(function() {
});

/******************************
 ***** Show error if have *****
 ******************************/
XGOON.Foo = {
    toAlert: 'test'
};
XGOON.Bar = function(arg)
{
    log(arg);

};

function log(msg) {
    if (!XGOON.isLog)
        return false;
    if (typeof (console) != 'undefined') {
        console.log(msg);
    }
}

window.onerror = function(msg, url, linenumber) {
    if (typeof (console) != 'undefined') {
        console.log('Error message: ' + msg + '\nURL: ' + url + '\nLine Number: ' + linenumber);
    }
    return false;
};

/******************************
 ***** Functions built in *****
 ******************************/

XGOON.exerciseDo = function() {
    if (jQuery('a.viewrule').length > 0) {
        var viewrule = jQuery('a.viewrule');
        viewrule.click(function(e) {
            if (jQuery('#mask').length == 0) {
                var articleid = jQuery(this).attr('alt');
                jQuery.ajax({
                    type: "GET",
                    data: "",
                    dataType: "text",
                    url: 'index.php?option=com_exercises&task=exercises.getTerm&termconditions=' + articleid,
                    success: function(response) {
                        if (response === '') {
                            alert('Sorry! Website cant load the term and conditions');
                        }
                        jQuery('body').append(response);
                        e.preventDefault();
                        var id = '.term-container';

                        //Get the A tag
                        //Get the screen height and width
                        var maskHeight = jQuery(document).height();
                        var maskWidth = jQuery(window).width();
                        //Set heigth and width to mask to fill up the whole screen
                        jQuery('#mask').css({
                            'width': maskWidth,
                            'height': maskHeight
                        });

                        //transition effect		
                        jQuery('#mask').fadeIn(1000);
                        jQuery('#mask').fadeTo("slow", 1);

                        //Get the window height and width
                        var winH = jQuery(window).height();
                        var winW = jQuery(window).width();

                        //Set the popup window to center
                        jQuery(id).css({
                            'top': winH / 2 - jQuery(id).height() / 2
                        });
                        jQuery(id).css('left', winW / 2 - jQuery(id).width() / 2);
                        jQuery(id).fadeIn(2000);
                    }
                });
                //Cancel the link behavior
            } else {
                var id = '.term-container';

                //Get the A tag
                //Get the screen height and width
                var maskHeight = jQuery(document).height();
                var maskWidth = jQuery(window).width();
                //Set heigth and width to mask to fill up the whole screen
                jQuery('#mask').css({
                    'width': maskWidth,
                    'height': maskHeight
                });

                //transition effect		
                jQuery('#mask').fadeIn(1000);
                jQuery('#mask').fadeTo("slow", 1);

                //Get the window height and width
                var winH = jQuery(window).height();
                var winW = jQuery(window).width();

                //Set the popup window to center
                jQuery(id).css({
                    'top': winH / 2 - jQuery(id).height() / 2
                });
                jQuery(id).css('left', winW / 2 - jQuery(id).width() / 2);
                jQuery(id).fadeIn(2000);
            }
        });
    }
};
removemask = function(element) {
    jQuery(element).fadeOut(500);
}
